package com.gh.shopmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gh.common.utils.PageUtils;
import com.gh.common.utils.Query;
import com.gh.shopmall.product.dao.CategoryBrandRelationDao;
import com.gh.shopmall.product.entity.BrandEntity;
import com.gh.shopmall.product.entity.CategoryBrandRelationEntity;
import com.gh.shopmall.product.entity.CategoryEntity;
import com.gh.shopmall.product.service.BrandService;
import com.gh.shopmall.product.service.CategoryBrandRelationService;
import com.gh.shopmall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        BrandEntity brandEntity = brandService.getById(brandId);
        CategoryEntity categoryEntity = categoryService.getById(catelogId);
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        baseMapper.insert(categoryBrandRelation);
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);
        baseMapper.update(categoryBrandRelationEntity, new QueryWrapper<CategoryBrandRelationEntity>().lambda()
                .eq(CategoryBrandRelationEntity::getBrandId, brandId));
    }

    @Override
    public void updateCategory(Long catId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setCatelogId(catId);
        categoryBrandRelationEntity.setCatelogName(name);
        baseMapper.update(categoryBrandRelationEntity, new QueryWrapper<CategoryBrandRelationEntity>().lambda()
                .eq(CategoryBrandRelationEntity::getCatelogId, catId));
    }

    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        List<BrandEntity> list = new ArrayList<>();
        //首先通过分类id在品牌分类关联表里面查询出品牌id
        List<Long> brandIdList = categoryBrandRelationService.list(new QueryWrapper<CategoryBrandRelationEntity>()
                .lambda()
                .eq(CategoryBrandRelationEntity::getCatelogId, catId)
        ).stream().map(CategoryBrandRelationEntity::getBrandId).collect(Collectors.toList());
        //然后通过品牌id查询出对应的品牌
        if (!CollectionUtils.isEmpty(brandIdList)) {
            brandIdList.forEach(brandId -> {
                BrandEntity brandEntity = brandService.getById(brandId);
                list.add(brandEntity);
            });
        }
        return list;
    }

}